package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.VideoManager;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class VideoController {

    @Autowired
    private VideoManager videoManager;

    // 查询推荐视频
    @GetMapping("/smallVideos")
    public PageBeanVo findVideo(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {

        Long userId = UserHolder.get().getId();

        return videoManager.findVideo(userId,pageNum, pageSize);
    }

    //上传视频
    @PostMapping(value = "/smallVideos",name = "0301")
    public void saveVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        Long userId = UserHolder.get().getId();
        videoManager.saveVideo(userId,videoThumbnail,videoFile);
    }
    //添加关注
    @PostMapping("/smallVideos/{uid}/userFocus")
    public void saveUserFocus(@PathVariable("uid") Long focusUserId){
        videoManager.saveUserFocus(focusUserId);
    }

    //删除关注
    @PostMapping("/smallVideos/{uid}/userUnFocus")
    public void deleteUserFocus(@PathVariable("uid") Long focusUserId){
        videoManager.deleteUserFocus(focusUserId);
    }


    //视频点赞
    @PostMapping("/smallVideos/{id}/like")
    public void saveVideoLike(@PathVariable("id") String videoId){
        videoManager.saveVideoLike(videoId);
    }

    //视频取消点赞
    @PostMapping("/smallVideos/{id}/dislike")
    public void deleteVideoComment(@PathVariable("id") String videoId){
        videoManager.deleteVideoComment(videoId);
    }

    //视频评论列表
    @GetMapping("/smallVideos/{id}/comments")
    public PageBeanVo videoCommentList(@PathVariable("id") String videoId,
         @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
         @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize){
        return videoManager.videoCommentList(videoId,pageNum,pageSize);
    }

    //视频评论发布
    @PostMapping("/smallVideos/{id}/comments")
    public void saveVideoComment(@PathVariable("id") String videoId,@RequestBody Map<String,String> map){

        String commentContent = map.get("comment");
       videoManager.saveVideoComment(videoId,commentContent);
    }

    //视频的评论点赞
    @PostMapping("/smallVideos/comments/{id}/like")
    public void videoCommentLike(@PathVariable("id") String commentId){

        videoManager.videoCommentLike(commentId);
    }

    //视频的评论点赞取消
    @PostMapping("/smallVideos/comments/{id}/dislike")
    public void videoCommentDislike(@PathVariable("id") String commentId){
        videoManager.videoCommentDislike(commentId);
    }
}

